/**
 * 
 */
package com.tvshows.front.listener.command.interfaces;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.hibernate.exception.JDBCConnectionException;

import com.tvshows.front.listener.command.AddFavoriteCommand;
import com.tvshows.front.listener.command.DeleteFavoriteCommand;
import com.tvshows.front.listener.command.ModifyFavoriteCommand;
import com.tvshows.front.ui.favorite.UpsideCompositeFavoriteUI;

/**
 * @author tMidy
 * <p>
 * The listener of the favorite view
 * </p>
 */
public class FavoriteCommandListener
    implements Listener
{

    /**
     * The {@link UpsideCompositeFavoriteUI}
     */
    private final UpsideCompositeFavoriteUI upsideComposite;

    /**
     * The {@link FavoriteCommand}
     */
    private FavoriteCommand command = null;

    /**
     * Logger of the class
     */
    private static final Logger LOGGER = Logger.getLogger( FavoriteCommandListener.class );

    /**
     * Constructeur
     */
    public FavoriteCommandListener( UpsideCompositeFavoriteUI upsideComposite )
    {
        this.upsideComposite = upsideComposite;
    }

    /**
     * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
     */
    @Override
    public void handleEvent( Event e )
    {
        try
        {
            if ( e.widget == upsideComposite.getAddFavorite() )
            {
                command = new AddFavoriteCommand( upsideComposite );
            }

            else if ( e.widget == upsideComposite.getDeleteFavorite() )
            {
                command = new DeleteFavoriteCommand( upsideComposite );
            }

            else if ( e.widget == upsideComposite.getModifyFavorite() )
            {
                command = new ModifyFavoriteCommand( upsideComposite );
            }

            command.executeCommand();
            LOGGER.info( "Commande " + command.getClass() + " exécutée " );
        }
        catch ( JDBCConnectionException exception )
        {
            LOGGER.error( exception );
            LOGGER.error( "Commande " + command.getClass() + " exécutée en erreur" );
        }
    }

}
